import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def test1(self):
        # 登录
        self.browser.get("http://listm:8080/admin/")
        sleep(1)
        self.browser.find_element(By.NAME, 'account').send_keys('admin')
        self.browser.find_element(By.NAME, 'password').send_keys('123456')
        self.browser.find_element(By.ID, 'login').click()
        sleep(2)
    def test_shop_list(self):
        # 商品列表
        # 点击商品
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a').click()
        sleep(1)
        # 查找子框架
        iframe1 = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        # 进入子框架
        self.browser.switch_to.frame(iframe1)
        # 定位商品名称输入框
        self.browser.find_element(By.ID, 'keyword').send_keys('文具')
        # 点击查询按钮
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]').click()
        sleep(2)
        # 定位商品列表
        tr_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
    def otest1(self):
        if len(tr_list) == 0:
         print('没有数据')
        else:
            is_not_bug = True

            for tr in tr_list:
        # 在tr查找td
                td = tr.find_element(By.XPATH, './td[2]/div')
                title = td.text
        # print(td.text)
        if '文具' not in title:
            is_not_bug = False
            print('有bug')
        if is_not_bug:
            print('没有bug')
            self.browser.switch_to.default_content()
            dt = self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[1]/ul[2]/li[3]/a/cite')
            ActionChains(self.browser).move_to_element(dt).perform()
            sleep(1)
            self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
            sleep(1)
    def test_shop_add(self):
            # 点击商品
            self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a').click()
            
            sleep(1)
            # 查找子框架
            iframe1 = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
            # 进入子框架
            self.browser.switch_to.frame(iframe1)
            # 定位发布商品按钮并点击
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
            sleep(2)
            # 查找商品发布子框架
            iframe_add = self.browser.find_element(By.XPATH, '//*[@id="layui-layer-iframe1"]')
            # 进入商品发布子框架
            self.browser.switch_to.frame(iframe_add)
            # 定位商品名称输入框并输入
            self.browser.find_element(By.NAME, 'name').send_keys('测试商品')
            sleep(1)
                # 定位第一个分类的选项框并点击
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
            sleep(1)
            # 定位第一个分类的下拉选项并点击
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
            sleep(1)
            # 定位第二个分类的选项框并点击
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
            sleep(1)
            # 定位第二个分类的下拉选项并点击
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
            sleep(1)
            # 定位第三个分类的选项框并点击
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
            sleep(1)
            # 定位第三个分类的下拉选项并点击
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
            sleep(1)
            # 定位商品主图的图片上传按钮并点击
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
            sleep(2)
            self.test_upload('//*[@id="layui-layer-iframe2"]','E:\\Xiao_Qi_he_zuo-Da4\\LikeShop\\r.jpg')
            # # 返回到商品子框架
            # self.browser.switch_to.parent_frame()
            # sleep(2)
            
            
           
            # # 定位图片上传子框架并切换
            # iframe_upload = self.browser.find_element(By.XPATH, )
            # self.browser.switch_to.frame(iframe_upload)
            # # 定位图片输入框并输入图片路径
            # self.browser.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/div[1]/input').send_keys()
            # sleep(4)
            # # 选择图片列表的第一张图片
            # self.browser.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/div[2]/div/ul/li[1]/div/img').click()
            
            # # 点击使用选中图片按钮
            # self.browser.find_element(By.XPATH, '//*[@id="used"]').click()



            # # 定位第三个分类的下拉选项并点击
            # self.browser.find_element(By.XPATH, "/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]").click()
            # sleep(1)
            # # 定位商品主图的图片上传按钮并点击
            # self.browser.find_element(By.XPATH, "/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a").click()
            # sleep(2)
            # self.test_upload('//*[@id="layui-layer-iframe2"]', "D:\\tyf\like\1.jpg")
            #   切换到商品发布子框架
            self.browser.switch_to.frame(iframe_add)
            sleep(2)
            
            # self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
            # 点击商品轮播图的图片上传按钮
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
            sleep(2)
            self.test_upload('//*[@id="layui-layer-iframe3"]', 'E:\\Xiao_Qi_he_zuo-Da4\\LikeShop\\r.jpg')
            # 切换到商品发布子框架
            self.browser.switch_to.frame(iframe_add)
            sleep(2)
            
            # 切换到价格库存标签页
            self.browser.find_element(By.XPATH, "/html/body/div/ul/li[2]").click()
            sleep(2)
            # 定位市场价（元）并输入
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[2]/input').send_keys(1)
            # 定位价格（元）并输入
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input').send_keys(1)
            # 定位成本价（元）并输
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[4]/input').send_keys(1)
            # 定位库存并输入
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input').send_keys(1)
            sleep(1)
            # 切换到销售设置标签页
            self.browser.find_element(By.XPATH, '/html/body/div/ul/li[4]').click()
            sleep(2)
            # 定位配送方式的快递发货并点击
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[4]/div[6]/div/div[1]/span').click()
            
            # 返回商品子框架
            self.browser.switch_to.parent_frame()
            sleep(2)
            # 定位保存按钮并点击
            self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
            sleep(2)
            

    def test_upload(self, xpath_path, img_path):
            """图片上传"""
            # 返回到商品子框架
            self.browser.switch_to.parent_frame()
            # 定位图片上传子框架并切换
            iframe_upload = self.browser.find_element(By.XPATH, xpath_path)
            self.browser.switch_to.frame(iframe_upload)
            # 定位图片输入框并输入图片路径
            self.browser.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/div[1]/input').send_keys(img_path)
            sleep(4)
            # 选择图片列表的第一张图片
            self.browser.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/div[2]/div/ul/li[1]/div/img').click()
            sleep(1)
            # 点击使用选中图片按钮
            self.browser.find_element(By.XPATH, "//*[@id='used']").click()
            # 返回到商品子框架
            self.browser.switch_to.parent_frame()
            
    def test_shop_online(self):
            # 商品上架测试
         # 点击商品
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a').click()
        sleep(1)
        # 查找子框架
        iframe1 = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        # 进入子框架
        self.browser.switch_to.frame(iframe1)
        # 定位到仓库中商品按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[2]').click()
        sleep(2)
        # 获取商品列表
        shop_list = self.browser.find_elements(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr')
        if len(shop_list) > 0:
             # 定位到商品列表中的第一个商品并点击上架
             shop_list[0].find_element(By.XPATH, './td/div/a[2]').click()
             sleep(2)
             # 定位确认按钮并点击
             self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
             sleep(2)
        else:
            print('仓库中暂无商品')
         
if __name__=='__main__':
    lk = LikeShopTest()
    lk.test1()
    # lk.test_shop_list()
    lk.test_shop_add()

    # lk.test_shop_online()